package com.yanqu.road.server.cmd.player.service;

import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.game.service.IGameServiceCommand;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.sync.message.SyncResponse;

public class GetUserBaseInfoServiceCmd implements IGameServiceCommand {

    @Override
    public SyncResponse execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        SyncResponse syncResponse = new SyncResponse();
        PlayerProto.CrossGetPlayerBaseInfoReqMsg reqMsg = PlayerProto.CrossGetPlayerBaseInfoReqMsg.parseFrom(request.getDataBytes());
        PlayerProto.CrossGetPlayerBaseInfoRespMsg.Builder respMsg = PlayerProto.CrossGetPlayerBaseInfoRespMsg.newBuilder();
        for (Long userId : reqMsg.getUserIdList()) {
            PlayerProto.CrossPlayerBaseInfoTemp.Builder builder = PlayerProto.CrossPlayerBaseInfoTemp.newBuilder();
            builder.setUserId(userId);
            UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(userId, GameServer.getInstance().getServerId());
            if(userBaseInfo != null) {
                builder.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                respMsg.addUserInfo(builder);
            }
        }
        syncResponse.setResponseBuilder(respMsg);
        syncResponse.setErrorCode(0);
        return syncResponse;
    }
}
